var basePath = getBasePath();
  $(function(){
	  loadCompanyTree();
	  var companyUUID = $("#companyUUID").val();
	  loadDeptTableList(companyUUID);
	  
	  //批量删除
	  $("#batchDeleteLink").bind("click",function(){
		  $.dataGridEasyUI.batchDeleteData({
			  url:getBasePath()+"/department/delMoreDepartment.do",
		  });
	  });
	
  })
  
  /**
   * 获取企业树数据
   * @returns
   */
  function loadCompanyTree(){
	  var daohangArea = $('#departmentListPage .daohangArea');
	  var url = getBasePath()+"company/getCompanyList4Tree.do"
	  daohangArea.tree({    
  	        url:url,
  	        method: 'post',
      		animate: true,
      		lines:true,
      		//单击某个节点触发的事件
      		onClick: function(node){
      			if(node.id!="0"){
      				loadDeptTableList(node.id);
      			}else{
      				loadDeptTableList("");
      			}
      		},
      });  
  }
  
  function loadDeptTableList(companyUUID){
	var placeHeight = $(".place").height();
	var windowHeight = $(window).height()-(placeHeight+30);
	console.log(companyUUID);
	if(companyUUID==''){
		companyUUID = "20170922140556660580775";
	}
	$("#companyUUID").val(companyUUID);
	//向后台加载数据
	tableObject.datagrid({
		height:windowHeight,
        url:basePath+'/department/getDepartmentList.do',
        //远程请求数据的方式
        method:"post",
        fit:false,
        //真正的自动展开/收缩列的大小，以适应网格的宽度，防止水平滚动。
        fitColumns:true,
        //调整列的位置
        resizeHandle:"right",
        //定义设置行的高度,根据该行的内容。设置为false可以提高负载性能
        autoRowHeight:"false",
        //工具栏
        toolbar:"#departmentTools .tb_toolbar",
        //boolean 是否显示斑马线效果。 
        striped:true,
        nowrap:false,
        idField:"id",
        rownumbers:true,//是否显示行号
        pagination:true,
        emptyMsg: '<span id="emptyMsg">无记录,点击左边的机构进行展示！</span>',
        loadMsg:"数据加载中，请稍后...",
        pageSize:15,//页长
        pageList:[15,20,30,40,50],
        sortOrder:"id",
        singleSelect:false,
        checkOnSelect:true,
        //定义查找数据字段
        queryParams: {
        	companyUUID:companyUUID
    	},
    	frozenColumns:[[
            {field:'id',checkbox:true},
            {field:'action',title:'操作',width:'160',formatter:function(value, row, index) {
            	var reg=new RegExp("#ID#","g"); //创建正则RegExp对象
            	return $("#departmentTools .row_toolbar").html().replace(reg,row.id);
			}}
    	]],
        columns:[[
            {field:'orderNumber',title:'顺序',width:'5%'},
            {field:'departmentName',title:'部门名称',width:'16%'},
            {field:'departmentDesc',title:'部门备注',width:'16%'},
            {field:'companyName',title:'所属公司',width:'16%'},
            {field:'editTime',title:'编辑时间',width:'16%',formatter:function(value, row, index) {
            	return formatByMillisecond(value, "yyyy-MM-dd HH:mm:ss");
			}}
        ]]
    });
  }
  
//添加数据
function addData(id){
    //验证是否登录--开始
	if(!checkLoginStatus()){return;}
    //验证是否登录--结束
    
	var editDataFormArea = $("#editDataFormArea");
	if(editDataFormArea.length==0){
		$("<div id='editDataFormArea'></div>").prependTo("body");
	}
    //得到左边树节点当前选中的项
	var curNode = $('#daohangArea').tree("getSelected");
	var companyUUID = $("#companyUUID").val();
	console.log("addData.companyUUID:"+companyUUID);
	if(curNode){companyUUID = curNode.companyUUID;}
    //计算距离
	var _width = 650;
	var _height = 220;
	var x_left = ($(window).width()-_width)/2;
	var y_top = ($(window).height()-_height)/2;
	$("#editDataFormArea").window({
	    title: '添加部门',
	    width: _width,
	    height: _height,
	    iconCls:'icon-window',
	    left:x_left,
	    top:y_top,
	    closed: false,
	    cache: false,
	    modal: true,
	    onClose:function(){
		    $("#editDataFormArea").html("");
		    $("#editDataFormArea").remove();
		},
		href:basePath+"manage/xtsz/editDepartment.jsp?id="+id+"&&companyUUID="+companyUUID
	});
}

//批量添加数据
function batchAddData(){
	//验证是否登录--开始
	if(!checkLoginStatus()){return;}
    //验证是否登录--结束
    
	var editDataFormArea = $("#editDataFormArea");
	if(editDataFormArea.length==0){
		$("<div id='editDataFormArea'></div>").prependTo("body");
	}
	//得到左边树节点当前选中的项
	var curNode = $('#daohangArea').tree("getSelected");
	var companyUUID = "";
	if(curNode){companyUUID = curNode.id;}
	
    //计算距离
	var _width = 650;
	var _height = 320;
	var x_left = ($(window).width()-_width)/2;
	var y_top = ($(window).height()-_height)/2;
	$("#editDataFormArea").window({
	    title: '批量添加部门',
	    width: _width,
	    height: _height,
	    iconCls:'icon-window',
	    left:x_left,
	    top:y_top,
	    closed: false,
	    cache: false,
	    modal: true,
	    onClose:function(){
		    $("#editDataFormArea").html("");
		    $("#editDataFormArea").remove();
		},
		href:basePath+"manage/xtsz/editAllDepartment.jsp?companyUUID="+companyUUID
	});
}
  
//删除单条数据	
function delData(id){
	//验证是否登录--开始
	if(!checkLoginStatus()){return;}
    //验证是否登录--结束
    
	$.messager.confirm('确认对话框', '此操作将永久删除该条数据,请确认是否继续操作？', function(flag){
		if (flag){
			$.post(basePath+"/department/delDepartment.do",{id:id},
	    	function(jsonData){
				if(jsonData.code=="00"){
					$.messager.alert('系统提示','删除成功！','success');
					tableObject.datagrid('reload');
				}else{
					$.messager.alert('系统提示',jsonData.msg,'error');
				}
	    	},"json");
		}
	});
}
